<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 120px;"
        class="filter-item"
        placeholder="请输入类型名"
        v-model="queryForm.typeName"
      >
      </el-input>
      <el-select
        clearable
        style="width: 125px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="请选择状态"
      >
        <el-option
          v-for="(val, key) in typeOptions"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button
        v-permission="'doorCardType:add'"
        class="filter-item"
        style="margin-left: 10px;"
        @click="openAddDialog"
        type="success"
        icon="el-icon-plus"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column
        prop="typeName"
        label="类型名"
        width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="expdate" label="有效期" width="250" align="center">
      </el-table-column>
      <el-table-column prop="cardFee" label="制卡费" width="150" align="center">
      </el-table-column>
      <el-table-column
        prop="isSale"
        label="是否线上购买"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.IS_TRUE[scope.row.isSale] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="isLimit"
        label="是否限购"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.IS_TRUE[scope.row.isLimit] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="limitNum"
        label="限购数量"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="stock" label="库存量" width="150" align="center">
      </el-table-column>
      <el-table-column
        prop="summary"
        label="类型摘要"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="introduce"
        label="类型介绍"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="status" label="状态" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.CONSUMER_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="modifyTime"
        label="修改时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="typeClass"
        label="类型分类"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.TYPE_CLASS[scope.row.typeClass] }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'doorCardType:update'"
            type="primary"
            size="mini"
            @click="editDoorCardType(scope.row)"
            title="编辑"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            v-permission="'doorCardType:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="'新增门卡类型'"
      :visible.sync="dialogAddFormVisible"
      width="50%"
    >
      <el-form
        :model="addForm"
        label-width="160px"
        :rules="addFormRules"
        ref="addForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型名" prop="typeName">
              <el-input
                v-model="addForm.typeName"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效期" prop="expdate">
              <el-date-picker
                v-model="addForm.expdate"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="请选择有效期"
                style="width: 100%;"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="制卡费" prop="cardFee">
              <el-input
                v-model="addForm.cardFee"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否线上购买" prop="isSale">
              <el-select
                v-model="addForm.isSale"
                placeholder=""
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.IS_TRUE"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="是否限购" prop="isLimit">
              <el-select
                v-model="addForm.isLimit"
                placeholder=""
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.IS_TRUE"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="限购数量" prop="limitNum">
              <el-input-number
                v-model="addForm.limitNum"
                :min="1"
                :max="99999999"
                style="width: 100%;"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="库存量" prop="stock">
              <el-input-number
                v-model="addForm.stock"
                :min="1"
                :max="99999999"
                style="width: 100%;"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卡类型" prop="typeClass">
              <el-select
                v-model="addForm.typeClass"
                placeholder=""
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.TYPE_CLASS"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="类型摘要" prop="summary">
              <el-input
                type="textarea"
                v-model="addForm.summary"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型介绍" prop="introduce">
              <el-input
                type="textarea"
                v-model="addForm.introduce"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="execAddForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="'编辑门卡类型'"
      :visible.sync="dialogEditFormVisible"
      width="50%"
    >
      <el-form
        :model="editForm"
        label-width="160px"
        :rules="editFormRules"
        ref="editForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型名" prop="typeName">
              <el-input
                v-model="editForm.typeName"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效期" prop="expdate">
              <el-date-picker
                v-model="editForm.expdate"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="请选择有效期"
                style="width: 100%;"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="制卡费" prop="cardFee">
              <el-input
                v-model="editForm.cardFee"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否线上购买" prop="isSale">
              <el-select
                v-model="editForm.isSale"
                placeholder=""
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.IS_TRUE"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="是否限购" prop="isLimit">
              <el-select
                v-model="editForm.isLimit"
                placeholder=""
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.IS_TRUE"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="限购数量" prop="limitNum">
              <el-input-number
                v-model="editForm.limitNum"
                :min="1"
                :max="99999999"
                style="width: 100%;"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="库存量" prop="stock">
              <el-input-number
                v-model="editForm.stock"
                :min="1"
                :max="99999999"
                style="width: 100%;"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="editForm.status"
                placeholder=""
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.CONSUMER_STATUS"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="类型摘要" prop="summary">
              <el-input
                type="textarea"
                v-model="editForm.summary"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型介绍" prop="introduce">
              <el-input
                type="textarea"
                v-model="editForm.introduce"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="execEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryDoorCardType,
  deleteDoorCardType,
  addDoorCardType,
  editDoorCardType
} from '@/api/card'

export default {
  name: 'doorCardType',
  data() {
    var checkSpecailSymbols = (rule, value, callback) => {
      if (!/[\u4e00-\u9fff\w]/g.test(value)) {
        callback(new Error('不支持特殊字符'))
      } else {
        callback()
      }
    }
    return {
      dialogAddFormVisible: false,
      dialogEditFormVisible: false,
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        typeName: undefined,
        status: undefined
      },
      addFormRules: {
        typeName: [
          { required: true, message: '请输入类型名', trigger: 'blur' },
          { validator: checkSpecailSymbols, trigger: 'blur' }
        ],
        expdate: [{ required: true, message: '请选择有效期', trigger: 'blur' }],
        cardFee: [{ required: true, message: '请输入制卡费', trigger: 'blur' }],
        isSale: [
          { required: true, message: '请选择是否线上购买', trigger: 'blur' }
        ],
        isLimit: [
          { required: true, message: '请选择是否限购', trigger: 'blur' }
        ],
        // limitNum: [
        //   { required: true, message: '请输入限购数量', trigger: 'blur' }
        // ],
        stock: [{ required: true, message: '请输入库存量', trigger: 'blur' }],
        typeClass: [
          { required: true, message: '请选择门卡类型', trigger: 'blur' }
        ]
      },
      addForm: {},
      editFormRules: {
        typeName: [
          { required: true, message: '请输入类型名', trigger: 'blur' },
          { validator: checkSpecailSymbols, trigger: 'blur' }
        ],
        expdate: [{ required: true, message: '请选择有效期', trigger: 'blur' }],
        cardFee: [{ required: true, message: '请输入制卡费', trigger: 'blur' }],
        isSale: [
          { required: true, message: '请选择是否线上购买', trigger: 'blur' }
        ],
        isLimit: [
          { required: true, message: '请选择是否限购', trigger: 'blur' }
        ],
        // limitNum: [
        //   { required: true, message: '请输入限购数量', trigger: 'blur' }
        // ],
        stock: [{ required: true, message: '请输入库存量', trigger: 'blur' }]
      },
      editForm: {},
      typeOptions: Dicts.CONSUMER_STATUS,
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryDoorCardType(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.records = data.records
        this.queryForm.size = data.size
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将删除该类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDoorCardType(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    openAddDialog() {
      this.addForm = {}
      this.dialogAddFormVisible = true
      this.$refs.addForm && this.$refs.addForm.resetFields()
    },
    execAddForm() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.addForm.status = '0'
          addDoorCardType(this.addForm).then(
            ({ data: { code, msg, data } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogAddFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    },
    openEditDialog() {
      this.editForm = {}
      this.dialogEditFormVisible = true
      this.$refs.editForm && this.$refs.editForm.resetFields()
    },
    editDoorCardType(row) {
      this.openEditDialog()
      this.editForm = {
        rowId: row.rowId,
        typeName: row.typeName,
        expdate: row.expdate,
        cardFee: row.cardFee,
        isSale: row.isSale.toString(),
        isLimit: row.isLimit.toString(),
        limitNum: row.limitNum,
        stock: row.stock,
        summary: row.summary,
        introduce: row.introduce,
        status: row.status
      }
    },
    execEditForm() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          editDoorCardType(this.editForm).then(
            ({ data: { code, msg, data } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogEditFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
